package com.weiaibenpao.demo.chislim.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseFragment;
import com.weiaibenpao.demo.chislim.ui.activity.moment.ReleaseActivity;
import com.weiaibenpao.demo.chislim.ui.adapter.RunningFragmentAdapter;
import com.weiaibenpao.demo.chislim.utils.TabLayoutIndicatorWeight;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/4.
 */

public class MomentFragment extends BaseFragment{


    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<String> titleList;
    private List<Fragment> fragmentLists;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_moment;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initList();
        viewPager.setAdapter(new RunningFragmentAdapter(getChildFragmentManager(), fragmentLists, titleList));
        tabLayout.setupWithViewPager(viewPager);
        TabLayoutIndicatorWeight.setIndicator(tabLayout, 43, 43);

    }

    private void initList() {
        titleList = new ArrayList<>();
        fragmentLists = new ArrayList<>();
        titleList.add("最新");
        titleList.add("关注");
        fragmentLists.add(MomentListFragment.newInstance(MomentListFragment.TYPE_LATEST));
        fragmentLists.add(MomentListFragment.newInstance(MomentListFragment.TYPE_FOLLOWED));
    }



    @OnClick(R.id.img_release)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.img_release:
                ReleaseActivity.startSelf(getContext(),this);
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            MomentListFragment fragment =(MomentListFragment) fragmentLists.get(0);
            if(requestCode == ReleaseActivity.REQUEST_POST_MOMENT){
                fragment.refresh();
            }
        }
    }
}
